//
//  YYENetworkTools.swift
//  weibo14
//
//  Created by mac on 16/3/4.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking
/// 请求方式的枚举

enum YYERequestMethod:String {

    case GET = "GET"
    case POST = "POST"

}

// 工具类 一个单利
class YYENetworkTools: AFHTTPSessionManager {

    // 定义一个返回值得类型
    typealias YYERequestCallback =  (responseObject:[String:AnyObject]? ,error :NSError?) -> ()
    
    
    static let sharedTools:YYENetworkTools = {
    
        let tools = YYENetworkTools()
        
        // 添加类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    func requst(method: YYERequestMethod = .GET , urlString: String, parameters: AnyObject?,
        callback:YYERequestCallback) {
    
            
            // 成功回掉
            let success = { (dataTask: NSURLSessionDataTask, responseObject: AnyObject?) in
            
                // 判断是否是一个字典
                guard let responseDict = responseObject as? [String:AnyObject] else {
                
                    // 如果发现不是字典
                    callback(responseObject: nil, error: NSError(domain: "com.itheima.hahaha", code: 10001, userInfo: ["message": "返回数据不是字典"]))
                    return
                }
                // 回调数据
                callback(responseObject: responseDict, error: nil)
            }
            
            
            
            
            let failure = { (dataTask: NSURLSessionDataTask?,error: NSError) in
            
                callback(responseObject:nil, error: error)
            }

            
  

            if method == .GET {
            
                self.GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
                
            } else {
            
            self.POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
            
            }
    }
}
extension YYENetworkTools{
    
    // 下载
    func upData(status:String,callback:YYERequestCallback) ->(){
    
        guard let accessToken =  YYEUserAccountViewModel.sharedAccountViewModel.accessToken else {
            callback(responseObject: nil, error: NSError(domain: "com.itheima", code: 1002, userInfo: ["errormsg": "accesstoken为nil"]))
            return
        }
        
        // 1. 定义url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        // 2. 定义参数
        let parameters = [
            "access_token": accessToken,
            "status": status //需要上传的文件值
        ]
        
        requst(.POST, urlString: urlString, parameters: parameters, callback: callback)
    }
    
    
    func upload(status: String, image: UIImage, callback: YYERequestCallback){
 
        guard let accessToken = YYEUserAccountViewModel.sharedAccountViewModel.accessToken else {
            callback(responseObject: nil, error: NSError(domain: "com.itheima", code: 1002, userInfo: ["errormsg": "accesstoken为nil"]))
            return
        }
        
        // 请求地址
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求方式
        let parameters = [
            "access_token" :accessToken,
            "status": status
        ]

        self.POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formdata) -> Void in

            let data = UIImagePNGRepresentation(image)!
            // 拼接图片数据需要的闭包
            /**
            - 参数1:name -> 接口里面对应该文件的字段
            - 参数2:fileName ->
            - 参数3:mimeType --> 告诉后台我要传给你的文件是什么类型的
            - 大类型/小类型
            - image/png text/html text/json
            - 如果不知道是什么类型的话: application/octet-stream
            */
            formdata.appendPartWithFileData(data, name: "pic", fileName: "aabb", mimeType: "application/octet-stream")
            
            }, success: { (_, responseObject) -> Void in
            
                
                guard let dict = responseObject as? [String: AnyObject] else{
                    // 如果发现不是字典
                    callback(responseObject: nil, error: NSError(domain: "com.itheima.hahaha", code: 10001, userInfo: ["message": "返回数据不是字典"]))
                    return
                }
                // 请求成功回调的闭包
                callback(responseObject: dict, error: nil)

                
            }) { (_, error) -> Void in
        
                // 请求失败回调的闭包
                callback(responseObject: nil, error: error)
        }
   
    }
    
    
}

